package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class RechargeYangYang extends BaseRepair {

    public RechargeYangYang(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String,String>> listMap = getData();
        DataMgr.addData(listMap);
        System.err.println(getGameServer().getServerInfo().getServerId()+":"+"跑行了");
    }

    private List<Map<String, String>> getData() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql =
                "SELECT u.userId,u.platformUserId,u.openId,FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S') as rgTime,l1.time as login1,l2.time as login2,l3.time as login3 " +
                " ,sum(r1.amount) as d1Recharge,sum(r2.amount) as d2Recharge,sum(r3.amount) as d3Recharge " +
                " FROM t_u_user as u" +
                " LEFT JOIN db_bussinessroad_log_"+getGameServer().getServerInfo().getServerId()+".t_l_login as l1 " +
                        "on TO_DAYS(FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S')) = TO_DAYS(l1.time) AND l1.userId = u.userId" +
                " LEFT JOIN db_bussinessroad_log_"+getGameServer().getServerInfo().getServerId()+".t_l_login as l2 " +
                        "on TO_DAYS(FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S')) + 1 = TO_DAYS(l2.time) AND l2.userId = u.userId" +
                " LEFT JOIN db_bussinessroad_log_"+getGameServer().getServerInfo().getServerId()+".t_l_login as l3 " +
                        "on TO_DAYS(FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S')) + 2 = TO_DAYS(l3.time) AND l3.userId = u.userId" +
                " LEFT JOIN t_u_recharge_detail as r1 " +
                        "on TO_DAYS(FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S')) = TO_DAYS(r1.createTime) AND r1.userId = u.userId AND r1.orderStatus != 0" +
                " LEFT JOIN t_u_recharge_detail as r2 " +
                        "on TO_DAYS(FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S'))+1 = TO_DAYS(r2.createTime) AND r2.userId = u.userId AND r2.orderStatus != 0" +
                " LEFT JOIN t_u_recharge_detail as r3 " +
                        "on TO_DAYS(FROM_UNIXTIME(u.registerTime,'%Y-%m-%d %H:%i:%S'))+2 = TO_DAYS(r3.createTime) AND r3.userId = u.userId AND r3.orderStatus != 0" +
                " WHERE registerTime >= 1661961600 AND registerTime <= 1663171200" +
                " GROUP BY(u.userId)";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("platformUserId",checkNullToEmpty(rs.getString("platformUserId")));
                temp.put("openId",checkNullToEmpty(rs.getString("openId")));
                temp.put("rgTime",checkNullToEmpty(rs.getString("rgTime")));
                temp.put("login1",checkNullToEmpty(rs.getString("login1")));
                temp.put("login2",checkNullToEmpty(rs.getString("login2")));
                temp.put("login3",checkNullToEmpty(rs.getString("login3")));
                temp.put("d1Recharge",checkNullToEmpty(rs.getString("d1Recharge")));
                temp.put("d2Recharge",checkNullToEmpty(rs.getString("d2Recharge")));
                temp.put("d3Recharge",checkNullToEmpty(rs.getString("d3Recharge")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

}
